﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RedditWP.Reddit;
using System.Windows.Media.Imaging;
using System.Threading;

namespace RedditWP
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            // callback so that we can construct the dynamic pics UI
            App.ViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ViewModel_PropertyChanged);

            TiltEffect.SetIsTiltEnabled((App.Current as App).RootFrame, true);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {

            if (e.PropertyName == "SubmissionList")
            {
                panoReddit.Visibility = Visibility.Visible;
                stackLoading.Visibility = Visibility.Collapsed;
                progressBar.IsIndeterminate = false;
            }
            else if (e.PropertyName == "PicsSubmissionList")
            {

                Thread t = new Thread(loadPics);
                t.Start();

            }
        }

        private void loadPics()
        {
            Submission orig;
            // add items to wrappanel
            //foreach (Submission submission in App.ViewModel.PicsSubmissionList)
            for (int cur = 0; cur < App.ViewModel.PicsSubmissionList.Count; cur++)
            {
                orig = App.ViewModel.PicsSubmissionList[cur];
                Submission submission = orig;

                Dispatcher.BeginInvoke(() =>
                {

                    if (!string.IsNullOrEmpty(submission.data.thumbnail))
                    {
                        try
                        {
                            Uri thumbnail = new Uri(submission.data.thumbnail);

                            Border b = new Border()
                            {
                                Width = 70,
                                Height = 70,
                                BorderThickness = new Thickness(2),
                                Margin = new Thickness(8)
                            };

                            Image i = new Image();

                            BitmapImage bitmapImage = new BitmapImage(thumbnail);

                            bitmapImage.CreateOptions = BitmapCreateOptions.BackgroundCreation;

                            i.Source = new BitmapImage(thumbnail);

                            b.Child = i;

                            wrapPics.Children.Add(b);
                        }
                        catch { } // some thumbnails are magic and don't have uris like "nsfw"
                    }
                });

                Thread.Sleep(250);
            }
        }

        /*
         *              Thread t = new Thread(AddPicSubmissionsThread);
                    t.Start();

                }
        }

        void AddPicSubmissionsThread()
        {
            try {
            Submission submission;

            // add items to wrappanel
            //foreach (Submission submission in App.ViewModel.PicsSubmissionList)
            for (int i = 0; i < App.ViewModel.PicsSubmissionList.Count; i++ )
            {
                submission = App.ViewModel.PicsSubmissionList[i];

                if (!string.IsNullOrEmpty(submission.data.thumbnail))
                {
                    try
                    {
                        Uri thumbnail = new Uri(submission.data.thumbnail);

                        Dispatcher.BeginInvoke(() =>
                        {
                            Uri thumb = thumbnail;
                            Border b = new Border()
                            {
                                Width = 70,
                                Height = 70,
                                BorderThickness = new Thickness(2),
                                Margin = new Thickness(8)
                            };

                            Image img = new Image();

                            BitmapImage bitmapImage = new BitmapImage(thumbnail);
                            bitmapImage.CreateOptions = BitmapCreateOptions.BackgroundCreation;

                            img.Source = new BitmapImage(thumbnail);

                            b.Child = img;

                            wrapPics.Children.Add(b);
                        });
                    }
                    catch { } // some thumbnails are magic and don't have uris like "nsfw"
                }

                Thread.Sleep(250);
            }
            } catch {} // cheating for the demo
        }
         * */

    }
}